<?php

namespace Products;

use Bank\BankUtils;

class ConsumerLoan extends Loan {
	const MIN_PERIOD = 1;
	const MAX_PERIOD = 60;
	const MIN_AMOUNT = 0.00;
	const MAX_AMOUNT = 20000.00;
	const CONSUMER_LOAN_INTERESTRATE = 10;
	private $period;
	private $amount;
	public function __construct($period, $amount) {
		$this->setPeriod ( $period );
		$this->setAmount ( $amount );
	}
	public function getPeriod() {
		return $this->period;
	}
	public function setPeriod($period) {
		if ($period == null) {
			throw new BaseException ( "Please enter consumer loan period!", BaseException::CODE_2007 );
		}
		if (is_int ( $period ) == false) {
			throw new BaseException ( "Please enter number for consumer loan period!", BaseException::CODE_2008 );
		}
		if ($period < self::MIN_PERIOD || $period > self::MAX_PERIOD) {
			throw new BaseException ( "Please enter period between 1 month and 60 months for consumer loan !", BaseException::CODE_2009 );
		}
		
		$this->period = $period;
	}
	public function getAmount() {
		return $this->amount;
	}
	public function setAmount($amount) {
		if ($amount == null) {
			throw new BaseException ( "Please enter consumer loan amount!", BaseException::CODE_2010 );
		}
		if (is_double ( $amount ) != true) {
			throw new BaseException ( "Please enter XXX.XX amount!", BaseException::CODE_2011 );
		}
		if ($amount < self::MIN_AMOUNT || $amount > self::MAX_AMOUNT) {
			throw new BaseException ( "Please enter amount between 0 and 20000!", BaseException::CODE_2012 );
		}
		
		$this->amount = $amount;
	}
	public function reduceAmount() {
		echo '<p>Reduce amount </p>';
		echo '<p>Consumer Loan - amount :' . $this->getAmount () . '</p>';
		echo '<p>Consumer Loan - monthly payment :' . BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::CONSUMER_LOAN_INTERESTRATE, $this->getPeriod () ) . '</p>';
		$this->setAmount ( $this->getAmount () - BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::CONSUMER_LOAN_INTERESTRATE, $this->getPeriod () ) );
		echo '<p>Consumer Loan - reduce amount :' . $this->getAmount () . '</p>';
	}
	public function monthlyPayment() {
		BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::CONSUMER_LOAN_INTERESTRATE, $this->getPeriod () );
	}
	public function showInfo() {
		echo '<p>Consumer Loan</p>';
		echo '<p>Consumer Loan - period : ' . $this->getPeriod () . '</p>';
		echo '<p>Consumer Loan - amount : ' . $this->getAmount () . '</p>';
		echo '<p>Consumer Loan - interestRate : ' . self::CONSUMER_LOAN_INTERESTRATE . ' % </p>';
		echo '<p>Consumer Loan - monthly payment : ' . BankUtils::calculateCreditMonthlyPayment ( $this->getAmount (), self::CONSUMER_LOAN_INTERESTRATE, $this->getPeriod () ) . ' BGN </p>';
	}
}
?>